package com.tianji.learning.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.StringUtils;
import com.tianji.learning.domain.entity.LearningLesson;
import com.tianji.learning.domain.entity.LearningRecord;
import com.tianji.learning.service.ILearningLessonService;
import com.tianji.learning.service.ILearningRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class LearningDelayMsgListener {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ILearningRecordService learningRecordService;
    @Autowired
    private ILearningLessonService lessonService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstants.Queue.LEARNING_DELAY_QUEUE),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_DELAY_EXCHANGE, type = ExchangeTypes.DIRECT, delayed = "true"),
            key = MqConstants.Key.LEARNING_DELAY_KEY
    ))
    public void onLearningDelayMsg(LearningRecord learningRecord) {
        log.info("----onLearningDelayMsg收到延迟消息：{}", JSONUtil.toJsonStr(learningRecord));
        LearningLesson learningLessonDB = lessonService.getById(learningRecord.getLessonId());
        //判断收到的和现在redis 中是否一致 一致就停止了观看就需要更新数据库，
        //获取redis中的学习记录
        String redisKey = learningRecordService.loadingKey(learningRecord.getUserId(), learningRecord.getLessonId(), learningRecord.getSectionId());
        log.info("---onLearningDelayMsg收到redisKey：{}",redisKey);
        String redisLearningRecordOnString = redisTemplate.opsForValue().get(redisKey);
        LearningRecord redisLearningRecord = JSONUtil.toBean(redisLearningRecordOnString, LearningRecord.class);
        log.info("---onLearningDelayMsg收到redis中的学习记录json：{}",JSONUtil.toJsonStr(redisLearningRecord));
        //收到延时消息看是否达到了50，达到50就需要更新learninglesson   在判断是否一致 决定是否更新 learningrecord
        //达到了50这一次  只更新一次learninglesson
        if (learningRecord.getFinished()&&learningRecord.getFirstFinished()){
            log.info("---onLearningDelayMsg第一次达到50了：{}");
            learningRecordService.checkFinishAndUpdataLesson(learningRecord.getSectionId(),learningLessonDB,learningLessonDB.getCourseId());
        }
            //一致就是停止了观看 更新 learningrecord 停止观看就更新数据库
        log.info("learningRecord.getMoment():{}",learningRecord.getMoment());
        log.info("redisLearningRecord.getMoment():{}",redisLearningRecord.getMoment());
        if (learningRecord.getMoment().equals(redisLearningRecord.getMoment())){
            log.info("---onLearningDelayMsg一致了：{}开始更新两个数据库");
            learningLessonDB.setUpdateTime(LocalDateTime.now());
            learningLessonDB.setLatestLearnTime(LocalDateTime.now());
            learningLessonDB.setLatestSectionId(learningRecord.getSectionId());
            lessonService.updateById(learningLessonDB);
            learningRecordService.update(learningRecord, learningRecordService.getQueryWrapper(learningRecord.getUserId(), learningRecord.getLessonId(), learningRecord.getSectionId()));
        }
    }
}
